﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tesla.Service.DataContracts;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.ServiceModel;
using Tesla.Service.WcfServiceCommon;
//using Telerik.Web.UI;
using Tesla.Util.Utilities;
using System.IO;

namespace Tesla.Web.Pages
{
    public partial class TaskDetails : PageBase
    {
        #region Class Level Variables
        private TaskServiceDispatcher taskDispatcher = null;
        private int? TaskRecordCount = 0;
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            List<TaskDC> task = null; IList<BaseDC> tasks = null, taskTypes = null, phases = null; 
            int taskID = 0;

            taskDispatcher = new TaskServiceDispatcher();

            try
            {
                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        task = taskDispatcher.ReadTask(Convert.ToInt32(Request.QueryString["ID"])).Cast<TaskDC>().ToList();
                        SetValuesForControls(task);
                        Legend1.InnerText = "Update Task";
                        taskID = Convert.ToInt32(Request.QueryString["ID"]);
                    }

                    tasks = taskDispatcher.ReadAllTasks(taskID).Cast<BaseDC>().ToList();
                    taskTypes = taskDispatcher.ReadAllTaskTypes().Cast<BaseDC>().ToList();
                    phases = taskDispatcher.ReadAllPhases().Cast<BaseDC>().ToList();

                    ArrayList arrTasks = new ArrayList(), arrTaskTypes = new ArrayList(), arrPhases = new ArrayList();

                    if (tasks != null && tasks.Count > 0)
                    {
                        foreach (BaseDC tDC in tasks)
                        {
                            arrTasks.Add((TaskDC)tDC);
                        }
                    }

                    if (taskTypes != null && taskTypes.Count > 0)
                    {
                        foreach (BaseDC ttDC in taskTypes)
                        {
                            arrTaskTypes.Add((TaskTypeDC)ttDC);
                        }
                    }

                    if (phases != null && phases.Count > 0)
                    {
                        foreach (BaseDC pDC in phases)
                        {
                            arrPhases.Add((PhaseDC)pDC);
                        }
                    }

                    lstParentTask.DataValueField = "TaskID"; lstParentTask.DataTextField = "TaskCode";
                    lstTaskType.DataValueField = "TaskTypeID"; lstTaskType.DataTextField = "TaskTypeCode";
                    lstPhase.DataValueField = "PhaseID"; lstPhase.DataTextField = "PhaseCode";
                    this.lstParentTask.DataSource = arrTasks;
                    this.lstTaskType.DataSource = arrTaskTypes;
                    this.lstPhase.DataSource = arrPhases;
                    this.lstParentTask.DataBind();
                    this.lstTaskType.DataBind();
                    this.lstPhase.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int? taskID = 0, success = 0;
                TaskDC createdTask = new TaskDC();
                createdTask.TaskCode = txtTaskCode.Text;
                createdTask.TaskDescription = txtTaskDescription.Text;
                createdTask.TaskTypeID = Convert.ToInt32(lstTaskType.SelectedItem.Value);
                createdTask.PhaseID = Convert.ToInt32(lstPhase.SelectedItem.Value);

                if(lstParentTask.SelectedItem != null)
                    createdTask.ParentTaskID = Convert.ToInt32(lstParentTask.SelectedItem.Value);
                createdTask.Task_PlannedEffort = Convert.ToInt32(txtTaskPlannedEffort.Text);
                createdTask.Task_AdditionalEffort = Convert.ToInt32(txtTaskAdditionalEffort.Text);
                taskDispatcher = new TaskServiceDispatcher();

                if (ViewState["SelectedTaskId"] == null)
                {
                    taskID = taskDispatcher.CreateTask(createdTask);
                }
                else
                {
                    createdTask.TaskID = Convert.ToInt32(ViewState["SelectedTaskId"]);
                    success = taskDispatcher.UpdateTask(createdTask);
                }
                if (!Page.ClientScript.IsClientScriptBlockRegistered("saveAlert"))
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "saveAlert", "alert('Data has been succesfully saved '); window.location.href('TaskSearch.aspx')", true);
                }


            }

            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SetValuesForControls(List<TaskDC> task)
        {
            ViewState["SelectedTaskId"] = ((TaskDC)task[0]).TaskID;
            this.txtTaskCode.Text = ((TaskDC)task[0]).TaskCode;
            this.txtTaskDescription.Text = ((TaskDC)task[0]).TaskDescription;
            this.txtTaskPlannedEffort.Text = ((TaskDC)task[0]).Task_PlannedEffort.ToString();
            this.txtTaskAdditionalEffort.Text = ((TaskDC)task[0]).Task_AdditionalEffort.ToString();
            
            if(((TaskDC)task[0]).ParentTaskID != 0)
                this.lstParentTask.SelectedValue = ((TaskDC)task[0]).ParentTaskID.ToString();

            if (((TaskDC)task[0]).TaskTypeID != 0)
                this.lstTaskType.SelectedValue = ((TaskDC)task[0]).TaskTypeID.ToString();

            if (((TaskDC)task[0]).PhaseID != 0)
                this.lstPhase.SelectedValue = ((TaskDC)task[0]).PhaseID.ToString();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect(VirtualPathUtility.ToAbsolute("~/") + @"Pages/Administrator/TaskSearch.aspx", false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
